<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<!--- <cfif NOT IsDefined("client.payments.orderBy") or resetFilters>
	<cfset client.payments.orderBy = "Last_Name">
<cfelseif IsDefined("orderBy")>
	<cfset client.payments.orderBy = orderBy>
</cfif>

<cfif NOT IsDefined("client.payments.orderDir") or resetFilters>
	<cfset client.payments.orderDir = "asc">
<cfelseif IsDefined("orderDir")>
	<cfset client.payments.orderDir = orderDir>
</cfif> --->

<cfif NOT IsDefined("client.payments.optOut") or resetFilters>
	<cfset client.payments.optOut = "All">
<cfelseif IsDefined("filterOptOut")>
	<cfset client.payments.optOut = filterOptOut>
</cfif>
<cfif client.payments.optOut neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Opt_Out = client.payments.optOut />
</cfif>

<cfif NOT IsDefined("client.payments.paymentType") or resetFilters>
	<cfset client.payments.paymentType = "All">
<cfelseif IsDefined("filterPaymentType")>
	<cfset client.payments.paymentType = filterPaymentType>
</cfif>
<cfif client.payments.paymentType neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Payment_Type = client.payments.paymentType />
</cfif>

<cfif NOT IsDefined("client.payments.authorized") or resetFilters>
	<cfset client.payments.authorized = "All">
<cfelseif IsDefined("filterAuthorized")>
	<cfset client.payments.authorized = filterAuthorized>
</cfif>
<cfif client.payments.authorized neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.authorized = client.payments.authorized />
</cfif>

<cfif NOT IsDefined("client.payments.cohort") or resetFilters>
	<cfset client.payments.cohort = 5>
<cfelseif IsDefined("filterCohort")>
	<cfset client.payments.cohort = filterCohort>
</cfif>
<cfif client.payments.cohort neq 5>
	<cfset isFiltered = true />
	<cfset queryParams.cohort = client.payments.cohort />
<cfelse>
	<cfset queryParams.cohort = 5 />
</cfif>

<cfif NOT IsDefined("client.payments.scholarshipType") or resetFilters>
	<cfset client.payments.scholarshipType = "All">
<cfelseif IsDefined("filterScholarshipType")>
	<cfset client.payments.scholarshipType = filterScholarshipType>
</cfif>
<cfif client.payments.scholarshipType neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.scholarshipType = client.payments.scholarshipType />
</cfif>

<cfif NOT IsDefined("client.payments.term") or resetFilters>
	<cfset client.payments.term = "All">
<cfelseif IsDefined("filterTerm")>
	<cfset client.payments.term = filterTerm>
</cfif>
<cfif client.payments.term neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.term = client.payments.term />
</cfif>

<cfif NOT IsDefined("client.payments.termType") or resetFilters>
	<cfset client.payments.termType = "All">
<cfelseif IsDefined("filterTermType")>
	<cfset client.payments.termType = filterTermType>
</cfif>
<cfif client.payments.termType neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.termType = client.payments.termType />
</cfif>

<cfif NOT IsDefined("client.payments.verificationType") or resetFilters>
	<cfset client.payments.verificationType = "All">
<cfelseif IsDefined("filterVerificationType")>
	<cfset client.payments.verificationType = filterVerificationType>
</cfif>
<cfif client.payments.verificationType neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.verificationType = client.payments.verificationType />
</cfif>

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.payments.queryParamsWDDX" />

<cfscript>
	base = new assets.calgrants.com.Base();
	paymentGateway = new assets.calgrants.com.gateway.PaymentGateway();
	payments = paymentGateway.query(argumentCollection = queryParams);
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	//paymentTypes = storedProcedureGateway.call("getPaymentTypes");
	paymentTypes = base.getPaymentTypes();
	cohorts = storedProcedureGateway.call("getDistinctCohorts");
	scholarshipTypes = storedProcedureGateway.call("getDistinctScholarshipTypes");
	terms = storedProcedureGateway.call("getDistinctTerms");
	termTypes = storedProcedureGateway.call("getDistinctTermTypes");
	verificationTypes = storedProcedureGateway.call("getVerificationTypes");
</cfscript>

<script type="text/javascript">
<!--
	var gridOnChange = function(a,b,c,d) {
		alert("gridOnChange triggered");
		console.log(a);
		console.log(b);
		console.log(c);
		console.log(d);
	}
	var gridOnError = function(a,b,c,d) {
		alert("gridOnError triggered");
		console.log(a);
		console.log(b);
		console.log(c);
		console.log(d);
	}
	var gridOnLoad = function(a,b,c,d) {
		alert("gridOnLoad triggered");
		console.log(a);
		console.log(b);
		console.log(c);
		console.log(d);
	}
//-->
</script>

<cfoutput>

	<h3>Payments CFGrid<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>

	<!--- <p>
		<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a><!--- <cfif isFiltered> <a href="#script_name#?view=payments&resetFilters=true" class="btn">Clear filters</a></cfif> --->
		<span class="pull-right"><strong>Count:</strong> #payments.RecordCount#</span>
	</p> --->
	
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif payments.RecordCount gt 0>
						<li><a href="?action=export&collection=payments" target="_new">Export</a></li>
					<cfelse>
						<li><a href="##">None - no records available with filter settings</a></li>
					</cfif>
				</ul>
			</div>
			<span class="pull-right"><strong>Count:</strong> #payments.RecordCount#</span>
		</div>
	</div>

	<cfform name="paymentsForm" id="paymentsForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		
		<div id="filter" class="well well-small" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<td>
						<label>Cohort</label>
						<cfselect name="filterCohort" size="1" multiple="false" required="yes" message="Cohort filter is required" style="width: 150px;">
							<cfloop query="cohorts">
								<option value="#Cohort#"<cfif client.payments.cohort eq Cohort> selected</cfif>>#Cohort#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Scholarship Type</label>
						<cfselect name="filterScholarshipType" size="1" multiple="false" required="yes" message="Scholarship Type filter is required" style="width: 150px;">
							<option value="All"<cfif client.payments.scholarshipType eq "All"> selected</cfif>>All</option>
							<cfloop query="scholarshipTypes">
								<option value="#Scholarship_Type#"<cfif client.payments.scholarshipType eq Scholarship_Type> selected</cfif>>#Scholarship_Type#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Term</label>
						<cfselect name="filterTerm" size="1" multiple="false" required="yes" message="Term filter is required" style="width: 150px;">
							<option value="All"<cfif client.payments.term eq "All"> selected</cfif>>All</option>
							<cfloop query="terms">
								<option value="#Term#"<cfif client.payments.term eq Term> selected</cfif>>#Term#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Term Type</label>
						<cfselect name="filterTermType" size="1" multiple="false" required="yes" message="Term Type filter is required" style="width: 150px;">
							<option value="All"<cfif client.payments.termType eq "All"> selected</cfif>>All</option>
							<cfloop query="termTypes">
								<option value="#Term_Type#"<cfif client.payments.termType eq Term_Type> selected</cfif>>#Term_Type#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Verification Type</label>
						<cfselect name="filterVerificationType" size="1" multiple="false" required="yes" message="Verification Type filter is required" style="width: 150px;">
							<option value="All"<cfif client.payments.verificationType eq "All"> selected</cfif>>All</option>
							<cfloop query="verificationTypes">
								<option value="#Verification_Type#"<cfif client.payments.verificationType eq Verification_Type> selected</cfif>>#Verification_Type#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Opt Out</label>
						<cfselect name="filterOptOut" size="1" multiple="false" required="yes" message="Opt Out filter is required" style="width: 150px;">
							<option value="All"<cfif client.payments.optOut eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.payments.optOut eq "Yes"> selected</cfif>>Yes</option>
							<option value="No"<cfif client.payments.optOut eq "No"> selected</cfif>>No</option>
						</cfselect>
					</td>
					<td>
						<label>Payment Type</label>
						<cfselect name="filterPaymentType" size="1" multiple="false" required="yes" message="Payment Type filter is required" style="width: 150px;">
							<option value="All"<cfif client.payments.paymentType eq "All"> selected</cfif>>All</option>
							<cfloop index="i" from="1" to="#ArrayLen(paymentTypes)#">
								<option value="#paymentTypes[i].value#"<cfif client.payments.paymentType eq paymentTypes[i].value> selected</cfif>>#paymentTypes[i].label#</option>
							</cfloop>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn">Apply</button> <a href="#script_name#?view=payments&resetFilters=true" class="btn">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif payments.RecordCount gt 0>
				
				<cfset colList = "Payment_ID,Cohort,Claim_ID,First_Name,Last_Name,Scholarship_Type,Term,Term_Type,Verification_Type,Approval_Date,Payment_Date,Payment_Amount" />
				<cfset editableColList = "Approval_Date,Payment_Date,Payment_Amount">
				<cfset colWidth = 1000 / ListLen(colList) />
				<cfset gridFont = "Open Sans" />
				<cfset gridFontSize = "14px" />
				
				<!--- pageSize doesn't work with HTML grid --->
		
				<cfgrid 	name="paymentsGrid" 
							height="500" 
							format="html" 
							autowidth="yes" 
							width="1880" 
							font="#gridFont#" 
							fontsize="#gridFontSize#" 
							query="payments" 
							selectmode="edit" 
							delete="no" 
							deletebutton="Delete Selected"
							selectOnLoad="no"
							stripeRows="yes">
							
					<cfgridcolumn 	name="Payment_ID" 
									font="#gridFont#" fontsize="#gridFontSize#" 
									header="ID" headermenu="yes" headerfont="#gridFont#" headerfontsize="#gridFontSize#" headerbold="yes" 
									select="no"
									href="?view=editPayment" hrefkey="Payment_ID" />
									
					<cfloop index="col" list="#colList#">
						<cfif ListFind(colList, col) and col neq "Payment_ID">
							<cfif ListFind(editableColList, col) gt 0>
								<cfset select = "yes" />
							<cfelse>
								<cfset select = "no" />
							</cfif>
							<cfgridcolumn 	name="#col#" 
											font="#gridFont#" fontsize="#gridFontSize#" 
											header="#Replace(col, '_', ' ', 'all')#" headermenu="no" headerfont="#gridFont#" headerfontsize="#gridFontSize#" headerbold="no" 
											select="#select#" />
						</cfif>
					</cfloop>
					
				</cfgrid>
		
			<!--- <table id="paymentsList" width="100%" style="display: none">
				<thead>
					<th>ID</th>
					<th>Cohort</th>
					<th>Claim ID</th>
					<th>Recipient</th>
					<th>Scholarship Type</th>
					<th>Term</th>
					<th>Term Type</th>
					<th>Verification Type</th>
					<th>Approval Date</th>
					<th>Payment Date</th>
					<th>Amount</th>
					<th>Status</th>
				</thead>
				<tbody>
					<cfloop query="payments">
						<tr>
							<td>
								<a href="?view=editPayment&Payment_ID=#Payment_ID#">#Payment_ID#</a>
								<!--- #Payment_ID#	 --->
							</td>
							<td>#Cohort#</td>
							<td>#Claim_ID#</td>
							<td>#First_Name# #Last_Name#</td>
							<td>#Scholarship_Type#</td>
							<td><span class="sortHide">#Sort_Order#</span>#Term#</td>
							<td>#Term_Type#</td>
							<td>#Verification_Type#</td>
							<td><span class="sortHide">#DateFormat(Approval_Date, 'yyyymmdd')#</span>#DateFormat(Approval_Date, "m/d/yyyy")#</td>
							<td>
								<cfif Opt_Out>
									Opt-out
								<cfelse>
									#DateFormat(Payment_Date, "m/d/yyyy")#
								</cfif>
							</td>
							<td>#DollarFormat(Payment_Amount)#</td>
							<td>#base.getPaymentStatus(Payment_ID = Payment_ID)#</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##paymentsList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						}			
					})
					$("##paymentsList").show();
				})
			</script> --->
			
		<cfelse>
		
			<p class="noRecords">No payments</p>
	
			<!--- <script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script> --->
			
		</cfif>

	</cfform>
	
</cfoutput>